﻿using CommonServiceLocator.StructureMapAdapter.Unofficial;
using Microsoft.Practices.ServiceLocation;
using NHibernate;
using PlusAssistance.DAL.Abstract;
using PlusAssistance.DAL.Concrete;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Pipeline;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace PlusAssistance.IoC
{
    public class IoCManager
    {
        public static Container Container { get; set; }

        public static void Initialize()
        {
            Container = new Container();

            Container.Configure(expression => expression.Scan(scanner =>
            {
                scanner.WithDefaultConventions();
                scanner.AssembliesFromApplicationBaseDirectory(filter => filter.FullName.StartsWith("PlusAssistance"));
            }));

            Container.Configure(cfg => cfg.For(typeof(IRepository<>)).Use(typeof(Repository<>)));

           // var con = Container.WhatDoIHave();
            var adapter = new StructureMapServiceLocator(Container);

            ServiceLocator.SetLocatorProvider(() => adapter);
        }

        public static void SetupSession(ISessionFactory sessionFactory)
        {
            Container.Configure(cfg =>
            {
                cfg.For<ISessionFactory>().Use(sessionFactory);
                cfg.For<ISession>().LifecycleIs(new ThreadLocalStorageLifecycle()).Use(sessionFactory.OpenSession());
            });
        }
    }
}
